package IO;

import java.io.*;
import java.util.Properties;
/*
* Properties :
* properties可以用来做配置文件
* javaweb   javaee 开发中通常会用到
*
* ResouceBundle 只读
* Properties 可读可写
*
* */
public class propertiesAPI {
    public static String version = "";
    public static String username = "";
    public static String password = "";
    //静态代码块,只会读取一次
    static {
     //   readConfig();
    }
    /*对属性文件的写入操作
    *
    * */
    private static void writeConfig(String version,String username,String password){
        Properties p = new Properties();
          p.put("app.version",version);
          p.put("db.username",username);
          p.put("db.password",password);
        try {
            OutputStream out = new FileOutputStream("com配置文件/Config.properties");
            p.store(out,"update config");
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    private static void readConfig(){
        Properties p = new Properties();
  //读取properties配置文件
        try {             //线程                  类加载器                   加载指定包下的配置文件
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("com配置文件/Config.properties");
       //     InputStream in = new FileInputStream("Config.properties");
            p.load(in);//加载文件Config.properties
            //从properties中读取数据
          version =  p.getProperty("app.version");
          username = p.getProperty("db.username");
          password = p.getProperty("db.password");
          in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
      //
     //   writeConfig("2","vince","12456");
        readConfig();
        System.out.println(version);
        System.out.println(username);
        System.out.println(password);

    }
}
